﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Security.Cryptography;
using System.Text;
using Tzx.Utilities;

namespace Tzx.Tools.Payroll2Images.Exe.Api
{
    /// <summary>
    /// 调用http://10.10.10.89:8900/api的远程API辅助类
    /// </summary>
    public class API_Config
    {
        private const string INI_section = "Api";

        private const string INI_key_ServerUrl = "ServerUrl";
        private const string INI_key_Url_Upload = "Url_Upload";
        private const string INI_key_Url_1_send = "Url_1_send";
        private const string INI_key_Url_2_progress = "Url_2_progress";
        private const string INI_key_Url_3_result = "Url_3_result";
        private const string INI_key_Url_4_recall = "Url_4_recall";

        #region token

        private const string INI_key_Token = "Token_v10";

        /// <summary>
        /// 读取INI的KEY,例如"0"
        /// </summary>
        private static string GetIniToken()
        {
            string ini_token = IniHelper.ReadIniData(INI_section, INI_key_Token, "#");

            //没有配置
            if (ini_token == "#")
            {
                const string _default_token = "server_public_token";
                IniHelper.WriteIniData(INI_section, INI_key_Token, _default_token);
                return _default_token;
            }

            return ini_token;
        }

        /// <summary>
        /// 本次访问服务端的Token密文，例如“f581b57120798a6c78ca5cd9e142b01d”
        /// </summary>
        /// <returns></returns>
        public static string Build_v10_Token()
        {
            string text_1 = string.Format("{0}\n{1}", GetIniToken(),DateTime.Now.ToString("yyyyMMdd"));
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bb_1 = Encoding.UTF8.GetBytes(text_1);
            byte[] bb_2 = md5.ComputeHash(bb_1);
            string text_2 = BitConverter.ToString(bb_2).Replace("-","");
            return text_2;
        }

        #endregion

        /// <summary>
        /// 服务器url,例如“http://10.10.10.89:8900/api/”
        /// </summary>
        public static string Url_Server
        {
            get
            {
                if (_Url_Server == null)
                {
                    string ini_url = IniHelper.ReadIniData(INI_section, INI_key_ServerUrl, "#");

                    //没有配置
                    if (ini_url == "#")
                    {
                        const string _default_server = "http://10.10.10.89:8900/api/";
                        IniHelper.WriteIniData(INI_section, INI_key_ServerUrl, _default_server);
                        return _default_server;
                    }

                    //
                    _Url_Server = ini_url;
                }
                return _Url_Server;
            }
        }
        private static string _Url_Server = null;

        /// <summary>
        /// 服务端对应api的url:上传，例如“ding_msg/media_uploadfile.ashx”
        /// </summary>
        public static string Url_1_Upload
        {
            get
            {
                if (_Url_1_Upload == null)
                {
                    string ini_url = IniHelper.ReadIniData(INI_section, INI_key_Url_Upload, "#");

                    //没有配置
                    if (ini_url == "#")
                    {
                        const string _default_api = "ding_msg/media_uploadfile.ashx";
                        IniHelper.WriteIniData(INI_section, INI_key_Url_Upload, _default_api);
                        return _default_api;
                    }
                    _Url_1_Upload = ini_url;
                }
                return _Url_1_Upload;
            }
        }
        private static string _Url_1_Upload = null;

        /// <summary>
        /// 服务端对应api的url:发送
        /// </summary>
        public static string Url_1_send
        {
            get
            {
                if (_Url_1_send == null)
                {
                    string ini_url = IniHelper.ReadIniData(INI_section, INI_key_Url_1_send, "#");

                    //没有配置
                    if (ini_url == "#")
                    {
                        const string _default_api = "ding_msg/MessageAsyncSend_1_send.ashx";
                        IniHelper.WriteIniData(INI_section, INI_key_Url_1_send, _default_api);
                        return _default_api;
                    }
                    _Url_1_send = ini_url;
                }
                return _Url_1_send;
            }
        }
        private static string _Url_1_send = null;

        /// <summary>
        /// 服务端对应api的url:进度
        /// </summary>
        public static string Url_2_progress
        {
            get
            {
                if (_Url_2_progress == null)
                {
                    string ini_url = IniHelper.ReadIniData(INI_section, INI_key_Url_2_progress, "#");

                    //没有配置
                    if (ini_url == "#")
                    {
                        const string _default_api = "ding_msg/MessageAsyncSend_2_progress.ashx";
                        IniHelper.WriteIniData(INI_section, INI_key_Url_2_progress, _default_api);
                        return _default_api;
                    }
                    _Url_2_progress = ini_url;
                }
                return _Url_2_progress;
            }
        }
        private static string _Url_2_progress = null;

        /// <summary>
        /// 服务端对应api的url:结果阅读状态
        /// </summary>
        public static string Url_3_result
        {
            get
            {
                if (_Url_3_result == null)
                {
                    string ini_url = IniHelper.ReadIniData(INI_section, INI_key_Url_3_result, "#");

                    //没有配置
                    if (ini_url == "#")
                    {
                        const string _default_api = "ding_msg/MessageAsyncSend_3_result.ashx";
                        IniHelper.WriteIniData(INI_section, INI_key_Url_3_result, _default_api);
                        return _default_api;
                    }
                    _Url_3_result = ini_url;
                }
                return _Url_3_result;
            }
        }
        private static string _Url_3_result = null;

        /// <summary>
        /// 服务端对应api的url:撤回消息
        /// </summary>
        public static string Url_4_recall
        {
            get
            {
                if (_Url_4_recall == null)
                {
                    string ini_url = IniHelper.ReadIniData(INI_section, INI_key_Url_4_recall, "#");

                    //没有配置
                    if (ini_url == "#")
                    {
                        const string _default_api = "ding_msg/MessageAsyncSend_4_recall.ashx";
                        IniHelper.WriteIniData(INI_section, INI_key_Url_4_recall, _default_api);
                        return _default_api;
                    }
                    _Url_4_recall = ini_url;
                }
                return _Url_4_recall;
            }
        }
        private static string _Url_4_recall = null;

    }
}